const cartmodel = require('../db/cart')
const respond = require('../utils/respond')
module.exports = {
    addcart: async (req, res) => {
        let {userid, goodsid, goodsnumber} = req.body
        // 存储到数据库
        // 判断数据库中是否有这个用户的这个商品
        let cartData = await cartmodel.findOne({userid, goodsid})
        if(cartData) {
            await cartmodel.updateOne({userid, goodsid}, {goodsnumber: cartData.goodsnumber+1})
            respond(res, 0, '添加成功')
        }  else {
            let docs = await cartmodel.insertMany([{
                userid,
                goodsid,
                goodsnumber
            }])
            if(docs.length === 0) {
                respond(res, 1, '添加失败')
                return
            }
            respond(res, 0, '添加成功')
        }
        
    }
}